import axios from "axios";
import { Message } from "view-design";

if (process.env.NODE_ENV === "development") {
  axios.defaults.baseURL = "http://47.114.143.104/api";
} else if (process.env.NODE_ENV === "debug") {
  axios.defaults.baseURL = "";
} else if (process.env.NODE_ENV === "production") {
  axios.defaults.baseURL = "http://47.114.143.104:8099/api";
}

const service = axios.create({
  timeout: 15000,
  withCredentials: true
});

service.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.code !== 200) {
      Message.error(res.data.message);
      return Promise.reject(res.data.message);
    } else {
      return res;
    }
  },
  error => {
    Message.error(error.data.message);
    return Promise.reject(error);
  }
);

export default service;
